package cn.tedu.nybikeT.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tedu.nybikeT.mapper.DataAnalysisMapper;
import cn.tedu.nybikeT.pojo.AnalyzedCrashDO;
import cn.tedu.nybikeT.pojo.InOutDO;
import cn.tedu.nybikeT.pojo.InOutVO;
import cn.tedu.nybikeT.pojo.PopularStationDO;
import cn.tedu.nybikeT.pojo.PopularStationVO;
import cn.tedu.nybikeT.pojo.StationInfoDO;
import cn.tedu.nybikeT.pojo.StationSecurityVO;
import cn.tedu.nybikeT.pojo.TopBikeDO;
import cn.tedu.nybikeT.pojo.TopBikeVO;
import cn.tedu.nybikeT.pojo.TripByUserDO;
import cn.tedu.nybikeT.pojo.TripByUserVO;
import cn.tedu.nybikeT.pojo.UnPopularStationDO;
import cn.tedu.nybikeT.service.IDataAnalysisService;
import cn.tedu.nybikeT.util.MapUtils;

@Service
public class DataAnalysisServiceImpl implements IDataAnalysisService {

	@Autowired
	DataAnalysisMapper mapper;

	// Rory
	@Override
	public List<StationSecurityVO> countStationSecurity(Integer year, Integer month, float radius) {

		/**
		 * 1.查询出所有站点数据 2.查询出某年份所有的事故数据 3.通过经纬度计算某个站点方圆n公里在某年发生的事故
		 * 4.为每一个站点生成StationSecurityVO对象
		 */
		radius *= 1000;// 公里转换为米
		List<StationSecurityVO> securityList = new ArrayList<>();
		List<StationInfoDO> stationInfoList = mapper.getAllStationInfoData();
		List<AnalyzedCrashDO> crashList = null;

		if (month == null) {
			crashList = mapper.getAnalyzedCrashDataByYear(year);
		}
		crashList = mapper.getAnalyzedCrashData(year, month);

		StationSecurityVO securityVO = null;
		StationInfoDO stationInfoDO = null;
		AnalyzedCrashDO crashDO = null;
		int summedInjuredNumber = 0;
		int summedKilledNumber = 0;
		int summedCrashNumber = 0;
		double summedDangerIndex = 0;
		for (int i = 0; i < stationInfoList.size(); i++) {
			securityVO = new StationSecurityVO();
			stationInfoDO = stationInfoList.get(i);
			securityVO.setStationBasicInfo(stationInfoDO);

			double longitudeFrom = stationInfoDO.getLon();
			double latitudeFrom = stationInfoDO.getLat();

			// 过经纬度计算一个站点方圆n公里在某年发生的事故
			for (int j = 0; j < crashList.size(); j++) {
				crashDO = crashList.get(j);
				double longitudeTo = crashDO.getLongitude();
				double latitudeTo = crashDO.getLatitude();

				if (MapUtils.getDistance(longitudeFrom, latitudeFrom, longitudeTo, latitudeTo) <= radius) {
					summedInjuredNumber += crashDO.getInjuredNumber();
					summedKilledNumber += crashDO.getKilledNumber();
					summedCrashNumber += 1;
					summedDangerIndex += crashDO.getDangerIndex();
				}
			}
			securityVO.setInjuredNumber(summedInjuredNumber);
			securityVO.setKilledNumber(summedKilledNumber);
			securityVO.setCrashNumber(summedCrashNumber);
			securityVO.setDangerIndex(summedDangerIndex);
			securityList.add(securityVO);

			summedInjuredNumber = 0;
			summedKilledNumber = 0;
			summedCrashNumber = 0;
			summedDangerIndex = 0;
		}

		return securityList;
	}
	// End

	// hk

	// End

	// late-whale

	// End

	// Asia
		@Override
		public TopBikeVO listGetTopBike() {
			// TODO Auto-generated method stub
			// 调用持久层方法查询数据
			List<TopBikeDO> topbikes = mapper.sqlGetBike();
			// 声明目标集合
			List<Integer> tripduration1 = new ArrayList<Integer>(5);
			List<Integer> bikeid1 = new ArrayList<Integer>(5);
			// 遍历数据封装到目标集合中
			// 添加综合操作数据
			for (TopBikeDO tb : topbikes) {
				tripduration1.add(tb.getTripduration());
				bikeid1.add(tb.getBikeid());
			}
			// 创建VO对象，将集合封装到VO中
			TopBikeVO vo = new TopBikeVO(tripduration1,bikeid1);
			// 返回VO对象
			return vo;
		}
		

		@Override
		public TripByUserVO listTripUserType() {
			// 调用持久层方法查询数据
			List<TripByUserDO> usertypes = mapper.sqlGetUserType();
			// 声明目标集合
			List<Integer> tripdur = new ArrayList<Integer>(2);
			List<String> usertype = new ArrayList<String>(2);
			// 遍历数据封装到目标集合中
			// 添加综合操作数据
			for (TripByUserDO ut : usertypes) {
				tripdur.add(ut.getTripDur());
				usertype.add(ut.getUserType());
			}
			// 创建VO对象，将集合封装到VO中
			TripByUserVO vo = new TripByUserVO(tripdur,usertype);
			// 返回VO对象
			return vo;
		}
	// End

	// LRY
	public PopularStationVO findPopularStationData(int topStationCountLimit, int lastStationCountLimit) {
		// 调用持久层方法查询数据
		List<PopularStationDO> psList = mapper.listPopularStationCount(topStationCountLimit);
		List<UnPopularStationDO> upsList = mapper.listUnPopularStationCount(lastStationCountLimit);

		// 声明目标集合
		List<Integer> topCountXData = new ArrayList<Integer>(topStationCountLimit);
		List<String> topCountNameData = new ArrayList<String>(topStationCountLimit);
		List<Integer> topCountYData = new ArrayList<Integer>(topStationCountLimit);
		List<Integer> lastCountXData = new ArrayList<Integer>(lastStationCountLimit);
		List<String> lastCountNameData = new ArrayList<String>(lastStationCountLimit);
		List<Integer> lastCountYData = new ArrayList<Integer>(lastStationCountLimit);

		// 遍历数据封装到目标集合中
		// 添加综合操作数据
		for (PopularStationDO ps : psList) {
			topCountXData.add(ps.getTopStationId());
			topCountNameData.add(ps.getTopStationName());
			topCountYData.add(ps.getCount());
		}

		for (UnPopularStationDO ups : upsList) {
			lastCountXData.add(ups.getLastStationId());
			lastCountNameData.add(ups.getLastStationName());
			lastCountYData.add(ups.getCount());
		}

		// 创建VO对象，将集合封装到VO中
		PopularStationVO vo = new PopularStationVO(topCountXData, topCountNameData, topCountYData, lastCountXData,
				lastCountNameData, lastCountYData);
		// 返回VO对象
		return vo;
	}

	public InOutVO findStationCountData(int stationCountLimit) {
		// 调用持久层方法查询数据
		List<InOutDO> ioList = mapper.listStationCount(stationCountLimit);

		// 声明目标集合
		List<String> stationNameXData = new ArrayList<String>(stationCountLimit);
		List<Integer> startStationYData = new ArrayList<Integer>(stationCountLimit);
		List<Integer> endStationYData = new ArrayList<Integer>(stationCountLimit);

		// 遍历数据封装到目标集合中
		// 添加综合操作数据
		for (InOutDO io : ioList) {
			stationNameXData.add(io.getStationName());
			startStationYData.add(io.getStartStationCount());
			endStationYData.add(io.getEndStationCount());
		}

		// 创建VO对象，将集合封装到VO中
		InOutVO vo = new InOutVO(stationNameXData, startStationYData, endStationYData);
		// 返回VO对象
		return vo;
	}
	// End
}
